package com.zjxh.zjxh.common.util;

import cn.hutool.core.util.StrUtil;
import com.zjxh.zjxh.common.constant.CMEnum;
import com.zjxh.zjxh.common.constant.Constant;
import com.zjxh.zjxh.common.exception.CommonException;
import java.util.Enumeration;
import java.util.LinkedHashMap;
import java.util.Map;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

/**
 * @description: 获取Request中token信息
 * @author: Oyp @Date: 2020/4/29 on 1:34 下午
 */
@Slf4j
public class RequestUtil {

  public static Long getUserId(HttpServletRequest request) {
    try {
      String userId = request.getHeader(Constant.HEADER_USER_ID);
      log.info("+++++++++++++++++++[{}]", userId);
      if (StrUtil.isBlank(userId)) {
        log.info("+++++++++++++++++++++++++没获取到userid");
        throw new CommonException(CMEnum.ERROR_TOKEN_VERIFY);
      }
      return Long.valueOf(userId);
    } catch (Exception e) {
      throw new CommonException(CMEnum.ERROR_TOKEN_VERIFY);
    }
  }

  public static String getToken(HttpServletRequest request) {
    try {
      Cookie[] cookies = request.getCookies();
      String token = null;
      if (cookies != null && cookies.length > 0) {
        for (Cookie cookie : cookies) {
          if (cookie.getName().equals(Constant.TOKEN)) {
            token = cookie.getValue();
          }
        }
      }
      if (token == null) {
        throw new CommonException(CMEnum.ERROR_TOKEN_VERIFY, "cookie中token为空");
      }
      return token;
    } catch (Exception e) {
      throw new CommonException(CMEnum.ERROR_TOKEN_VERIFY);
    }
  }

  public static String getPlatform(HttpServletRequest request) {
    try {
      String platform = request.getHeader(Constant.PLATFORM);
      log.info("+++++++++++++++++++[{}]", platform);
      if (StrUtil.isBlank(platform)) {
        log.info("+++++++++++++++++++++++++没获取到platform");
        throw new CommonException(CMEnum.ERROR_TOKEN_VERIFY);
      }
      return platform;
    } catch (Exception e) {
      throw new CommonException(CMEnum.ERROR_TOKEN_VERIFY);
    }
  }

  /** 获取request */
  public static HttpServletRequest getRequest() {
    try {
      return getRequestAttributes().getRequest();
    } catch (Exception e) {
      return null;
    }
  }

  /** 获取response */
  public static HttpServletResponse getResponse() {
    try {
      return getRequestAttributes().getResponse();
    } catch (Exception e) {
      return null;
    }
  }

  /**
   * 获取request:异步无法获取到
   *
   * @return
   */
  public static ServletRequestAttributes getRequestAttributes() {
    try {
      return (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
    } catch (Exception e) {
      log.info("获取ServletRequestAttributes 异常：", e);
      return null;
    }
  }

  /**
   * 获取header
   *
   * @param request
   * @return
   */
  public static Map<String, String> getHeaders(HttpServletRequest request) {
    Map<String, String> map = new LinkedHashMap<>();
    if (request == null) {
      return map;
    }
    Enumeration<String> enumeration = request.getHeaderNames();
    if (enumeration != null) {
      while (enumeration.hasMoreElements()) {
        String key = enumeration.nextElement();
        String value = request.getHeader(key);
        map.put(key, value);
      }
    }
    return map;
  }
}
